package com.lzp.badminton.service.impl;

import com.lzp.badminton.model.Activity;
import com.lzp.badminton.model.Activityjoin;
import com.lzp.badminton.mapper.ActivityjoinMapper;
import com.lzp.badminton.service.IActivityService;
import com.lzp.badminton.service.IActivityjoinService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzp.badminton.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2022-02-11
 */
@Service
public class ActivityjoinServiceImpl extends ServiceImpl<ActivityjoinMapper, Activityjoin> implements IActivityjoinService {
    @Autowired
    private ActivityjoinMapper activityjoinMapper;
    @Autowired
    private IActivityService iActivityService;

    // 获取某一活动的报名数据
    public List<Activityjoin> getActivityJoin(Integer activityId) {
        Map ajMap = new HashMap();
        ajMap.put("activity_id", activityId);

        List<Activityjoin> activityjoinList = activityjoinMapper.selectByMap(ajMap);
        return activityjoinList;
    }

    // 获取某一活动的报名人数
    public int getJoinNum(Integer activityId) {
        Map ajMap = new HashMap();
        ajMap.put("activity_id", activityId);
        List<Activityjoin> activityjoinList = activityjoinMapper.selectByMap(ajMap);

        // 遍历list，加到报名人数
        if (activityjoinList == null) {
            return 0;
        } else {
            int result = 0;
            for (Activityjoin aj: activityjoinList) {
                result += aj.getJoinNum();
            }
            return result;
        }
    }

    // 获取某个用户是否参与了某一个活动
    public Activityjoin getJoinActivityUser(String userid, Integer activityId) {
        Map ajMap = new HashMap();
        ajMap.put("activity_id", activityId);
        ajMap.put("user_id", userid);
        List<Activityjoin> result = activityjoinMapper.selectByMap(ajMap);

        System.out.println(result+"111111111111111");

        if (result.size() != 0) {
            return result.get(0);
        } else {
            // 用户没有报名
            return null;
        }
    }

    // 用户取消报名
    public int cancelUserJoin(String userid, Integer activityId) {
        Map ajMap = new HashMap();
        ajMap.put("activity_id", activityId);
        ajMap.put("user_id", userid);

        int result = activityjoinMapper.deleteByMap(ajMap);

        return result;
    }

    // 用户报名活动
    public int userJoinActivity(String userid, Integer activityId, Integer joinNum) {
        // 判断报名人数是否超额
        Activity activity = iActivityService.getoneActivityInfoById(activityId);
        int limitNum = activity.getLimitNum();

        if (joinNum + getJoinNum(activityId) > limitNum) {
            return -1;
        }

        // 获取当前时间
        String joinDate = new DateUtils().getCurrentDate();

        Activityjoin aj = new Activityjoin();
        aj.setActivityId(activityId);
        aj.setJoinTime(joinDate);
        aj.setUserId(userid);
        aj.setJoinNum(joinNum);

        int result = activityjoinMapper.insert(aj);

        return result;
    }
}
